package com.example.boxugu.myapplication.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.boxugu.myapplication.Moder.LoginModer;
import com.example.boxugu.myapplication.R;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class RegisterActivity extends AppCompatActivity {
    Button mBtn2;
    EditText mUsername2,mPassword2,mPassword3;
    ImageButton mImgbtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        mBtn2=findViewById(R.id.rgister_btn);
        mUsername2=findViewById(R.id.edit_username2);
        mPassword2=findViewById(R.id.edit_passwrod21);
        mPassword3=findViewById(R.id.edit_passwrod3);
        mImgbtn=findViewById(R.id.imgbtn_back);
        mImgbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mBtn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RequestParams requestParams=new RequestParams("http://148.70.46.9:8080/boxuegu/bxg/register");
                String username=mUsername2.getText().toString();
                String password=mPassword2.getText().toString();
                String password2=mPassword3.getText().toString();
                requestParams.addBodyParameter("username",username);
                if (TextUtils.isEmpty(username)){
                    Toast.makeText(RegisterActivity.this,"请输入用户名",Toast.LENGTH_LONG).show();
                    return;
                }if(TextUtils.isEmpty(password)){
                    Toast.makeText(RegisterActivity.this,"请输入密码",Toast.LENGTH_LONG).show();
                    return;
                }if(TextUtils.isEmpty(password2)){
                    Toast.makeText(RegisterActivity.this,"请再次输入密码",Toast.LENGTH_LONG).show();
                    return;
                }
                if (!password.equals(password2)){
                    Toast.makeText(RegisterActivity.this,"两次的密码不对",Toast.LENGTH_LONG).show();
                    return;
                }
                requestParams.addBodyParameter("password",password);
                x.http().post(requestParams, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        Gson gson =new Gson();
                        LoginModer moder=gson.fromJson(result,LoginModer.class);
                        Log.i("RegisterActivity","sucess"+result);
                        if (moder.code==0){
                            Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_LONG).show();
                            Intent intent=new Intent(RegisterActivity.this,loginActivity.class);
                            startActivity(intent);
                            finish();
                        }else {
                            Toast.makeText(RegisterActivity.this,"注册失败",Toast.LENGTH_LONG).show();
                        }

                    }
                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        Log.i("RegisterActivity","error"+ex.getMessage());
                    }
                    @Override
                    public void onCancelled(Callback.CancelledException cex) {
                    }
                    @Override
                    public void onFinished() {
                    }
                });
            }
        });
    }
}
